package com.ray.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.auth.MalformedChallengeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.ListStringBean;
import com.ray.bean.PermissionBean;
import com.ray.bean.RoleBean;
import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.service.BasicDataService;
import com.ray.service.PermissionService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

@Controller
@RequestMapping("permission")
public class PermissionController {
	@Autowired
	PermissionService permissionService;
	@Autowired
	BasicDataService basicDataService;

	
//	添加权限
	@RequestMapping("/addPermission")
	@ResponseBody
	public Map addPermission(PermissionBean PermissionBean,HttpServletRequest request) throws ServiceException {
		MyUser myuser=RequestUtil.getLoginUser(request);
		PermissionBean.setCreateUser(myuser.getUserId());
		permissionService.addPermission(PermissionBean);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
//	修改权限
	@RequestMapping("/updatePermission")
	@ResponseBody
	public Map updatePermission(PermissionBean PermissionBean,HttpServletRequest request) throws ServiceException {
		MyUser myuser=RequestUtil.getLoginUser(request);
		PermissionBean.setCreateUser(myuser.getUserId());
		permissionService.updatePermission(PermissionBean);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
//	查询所有权限
	@RequestMapping("/querryAllPermission")
	@ResponseBody
	public Map querryAllPermission() throws ServiceException {
		return ResponseUtil.ResponseResult(permissionService.querryAllPermission());
	}
	
//	查询所有权限
	@RequestMapping("/querryAllrole")
	@ResponseBody
	public Map querryAllrole(HttpServletRequest request) throws ServiceException {
		MyUser myuser=RequestUtil.getLoginUser(request);
		return ResponseUtil.ResponseResult(permissionService.querryAllrole(myuser.getCompany()));
	}
	

//	添加权限
	@RequestMapping("/addrole")
	@ResponseBody
	public Map addrole(RoleBean roleBean,HttpServletRequest request) throws ServiceException {
		MyUser myuser=RequestUtil.getLoginUser(request);
		roleBean.setCreateUser(myuser.getUserId());
		roleBean.setCompanyId(myuser.getCompany());
		permissionService.addRole(roleBean);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
	
//	获取角色的权限
	@RequestMapping("/getRolePermission")
	@ResponseBody
	public Map getRolePermission(String roleId,HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(permissionService.getRolePermission(roleId,map));
	}
	
//	获取角色没有的权限
	@RequestMapping("/getRoleNoPermission")
	@ResponseBody
	public Map getRoleNoPermission(String roleId,HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(permissionService.getRoleNoPermission(roleId,map));
	}
	
//	添加角色权限
	@RequestMapping("/addRolePermission")
	@ResponseBody
	public Map addRolePermission(String roleId,String permissionIdArray,HttpServletRequest request) throws ServiceException {
		MyUser myUser =RequestUtil.getLoginUser(request);
		permissionService.addRolePermission(roleId,permissionIdArray);
		permissionService.setUserRolePermission(roleId,myUser,request);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
//	移除角色权限
	@RequestMapping("/removePermission")
	@ResponseBody
	public Map removePermission(String roleId,String permissionIdArray,HttpServletRequest request) throws ServiceException {
		MyUser myUser =RequestUtil.getLoginUser(request);
		permissionService.removePermission(roleId,permissionIdArray);
		permissionService.setUserRolePermission(roleId,myUser,request);
		return ResponseUtil.ResponseResult(ReturnStringUtil.REMOVE_SUCCESS);
	}
	
//	获取用户角色
	@RequestMapping("/getUserRole")
	@ResponseBody
	public Map getUserRole(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(permissionService.getUserRole(map));
	}
	
//	获取用户没有的角色
	@RequestMapping("/getUserNoRole")
	@ResponseBody
	public Map getUserNoRole(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(permissionService.getUserNoRole(map));
	}
	
//	增加用户角色
	@RequestMapping("/addUserRole")
	@ResponseBody
	public Map addUserRole(String userId,String roleIdArray,HttpServletRequest request) throws ServiceException {
		MyUser myUser=RequestUtil.getLoginUser(request);
		permissionService.addUserRole(userId,roleIdArray);
		if(myUser.getUserId().equals(userId)) {//如果修改的是登录人的权限
			myUser.setPermissionList(permissionService.getUserPermissionBeanByUserId(myUser.getUserId()));
			request.getSession().setAttribute("userLogin", myUser);	
		}
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
//	移除用户角色
	@RequestMapping("/removeUserRole")
	@ResponseBody
	public Map removeUserRole(String userId,String roleIdArray,HttpServletRequest request) throws ServiceException {
		MyUser myUser=RequestUtil.getLoginUser(request);
		permissionService.removeUserRole(userId,roleIdArray);
		if(myUser.getUserId().equals(userId)) {//如果修改的是登录人的权限
			myUser.setPermissionList(permissionService.getUserPermissionBeanByUserId(myUser.getUserId()));
			request.getSession().setAttribute("userLogin", myUser);	
		}
		return ResponseUtil.ResponseResult(ReturnStringUtil.REMOVE_SUCCESS);
	}
	
//	删除角色
	@RequestMapping("/deleteRole")
	@ResponseBody
	public Map deleteRole(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		permissionService.deleteRole(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
//	删除角色
	@RequestMapping("/updateRole")
	@ResponseBody
	public Map updateRole(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		permissionService.updateRole(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
//	获取用户权限
	@RequestMapping("/getUserPermissionBeanByUserId")
	@ResponseBody
	public Map getUserPermissionBeanByUserId(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(permissionService.getUserPermissionBeanByUserId(map.get("loginUserId").toString()));
	}
	
	
//	获取权限用户
	@RequestMapping("/getRoleUser")
	@ResponseBody
	public Map getRoleUser(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(permissionService.getRoleUser(map));
	}
	
	
//	获取公司权限
	@RequestMapping("/getCompanyPermission")
	@ResponseBody
	public Map getCompanyPermission(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(permissionService.getCompanyPermission(map));
	}
	
	
//	获取公司权限
	@RequestMapping("/setCompanyPermission")
	@ResponseBody
	public Map setCompanyPermission(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(permissionService.setCompanyPermission(map));
	}
	
	
	
	
}
